package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.DepartmentDao;
import com.djinfo.work.dao.UserDao;
import com.djinfo.work.dao.impl.DepartmentDaoImpl;
import com.djinfo.work.entity.Department;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lenovo on 2017/5/17.
 */
@Service("DepartmentService")
public class DepartmentServiceImpl extends BaseServiceImpl<Department,DepartmentDaoImpl> implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private UserDao userDao;
    @Override
    public Department getDepartmentById(String id) {
        return departmentDao.getDepartmentById(id);
    }

    @Override
    public List<Department> page(Department object, Integer pageNum, Integer pageSize) {
        return departmentDao.page(object,pageNum,pageSize);
    }

    @Override
    public List<Department> list() {
        return departmentDao.list();
    }

    @Override
    public Integer getAllRowCount(Department department) {
        return departmentDao.getAllRowCount(department);
    }

    @Override
    public List<Department> getDepartsBy(String parentid) {
        return departmentDao.getDepartsBy(parentid);
    }

    @Override
    public List<Department> getTwoDepartid() {
        return departmentDao.getTwoDepartid();
    }

    @Override
    public List<Department> getOneDepartid() {
        return departmentDao.getOneDepartid();
    }

    @Override
    public List<Department> setTwoToOne(String deptid) {
        return departmentDao.setTwoToOne(deptid);
    }

    @Override
    public List<Department> getLevel1DepartmentList() {
        return departmentDao.getLevel1DepartList();
    }

    @Override
    public List<Department> getLevel2DepartmentList() {
        return departmentDao.getLevel2DepartList();
    }

    @Override
    public void deleteChildDepartmentByParentId(String pid) {
        departmentDao.deleteChildByParentDepartId(pid);
    }

    @Override
    public AjaxJson list(Department department, Integer pagenum, Integer pagesize) {
        AjaxJson ajaxJson = null;
        try {
            List list=new ArrayList();
            HashMap<String, Object> map = new HashMap<>();
            ajaxJson = new AjaxJson();
            department.setParentDeptId("0");
            List<Department> res = departmentDao.page(department,pagenum,pagesize);
            for(Department department1:res){
                HashMap<String, Object> map2 = new HashMap<>();
                String id=department1.getId();
                Department department2=new Department();
                department2.setParentDeptId(id);
                Integer pagenum2=1;
                Integer pagesize2=20;
                List<Department> res2 = departmentDao.page(department2,pagenum2,pagesize2);
                list.add(department1);
                for(Department department3:res2){
                    list.add(department3);
                }
            }
            Integer count = departmentDao.getAllRowCount(department);
            map.put("result",list);
            map.put("total",count);
            ajaxJson.setResult(map);
        } catch (Exception e) {
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson deptList(String uid) {
        AjaxJson ajaxJson=new AjaxJson();
        try {
            HashMap map=new HashMap();
            List<Department> level1List=departmentDao.getLevel1DepartList();//获取所有的一级部门
            List<Department> level2List=departmentDao.getLevel2DepartList();//获取所有的二级部门
            TbSysUsers res = userDao.get(uid);
            Department level1=new Department();
            Department level2=new Department();
            if(res!=null){
                String did=res.getDeptId();
                if(did!=null&&!"".equals(did)) {
                    level2 = departmentDao.getDepartmentById(did);
                    if (level2 != null) {
                        String did2 = level2.getParentDeptId();
                        if (did2 != null && !"".equals(did2)) {
                            level1 = departmentDao.getDepartmentById(did2);
                        }
                    }
                }
            }
            map.put("level1List",level1List);
            map.put("level2List",level2List);
            map.put("level1",level1);
            map.put("level2",level2);
            ajaxJson.setResult(map);
        } catch (Exception e) {
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson getDeptList() {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            Department department=new Department();
            department.setDeptName("将该部门设定为一级");
            department.setId("0");
            List list=departmentDao.getLevel1DepartList();
            list.add(0,department);
            HashMap map=new HashMap();
            map.put("deptList",list);
            ajaxJson.setResult(map);
        } catch (Exception e) {
            ajaxJson.setCode(301);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    @Override
    public AjaxJson saveOrUpdate(Department department, HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            if(department.getId()==null||"".equals(department.getId())){
                department.setId(null);
                String nowTime = StringUtil.getNowTime();
                department.setCreateTime(nowTime);
                department.setUpdateTime(nowTime);
                departmentDao.save(department);
            }else {
                String nowTime = StringUtil.getNowTime();
                department.setUpdateTime(nowTime);
                departmentDao.update(department);
            }
        } catch (Exception e) {
            ajaxJson.setCode(301);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }

        return ajaxJson;
    }

    @Override
    public AjaxJson deleteDept(Department department) {
        AjaxJson ajaxJson = new AjaxJson();
        department=departmentDao.getDepartmentById(department.getId());

        if("0".equals(department.getParentDeptId())){
                departmentDao.deleteChildByParentDepartId(department.getId());
        }
        departmentDao.delete(department);
        ajaxJson.setCode(101);
        ajaxJson.setMsg("success");
        return ajaxJson;
    }

    @Override
    public AjaxJson deptList(Department department) {
        AjaxJson ajaxJson = new AjaxJson();
        List list=departmentDao.list();
        ajaxJson.setCode(101);
        HashMap<String, Object> map =new HashMap();
        map.put("list",list);
        ajaxJson.setResult(map);
        return ajaxJson;
    }
}
